package com.bj58.ecdata.dashboard.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.InfoConst;
import com.bj58.ecdata.dashboard.constant.InfoSourceConst;
import com.bj58.ecdata.dashboard.databll.DatainfoBLL;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.dbentity.InfoCore;
import com.bj58.ecdata.dashboard.dbentity.InfoResume_age;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

/**
 * 信息看板Controller
 * @author zhaoxiang 2015-07-21
 */
@Path("/info")
public class InfoController extends MvcController {
	private SomePublicUtil spu = new SomePublicUtil();
	private AbstractBaseService<InfoCore> infoService =ServiceBeanFactory.get(InfoCore.class,null);
	private AbstractBaseService<InfoResume_age> infoResumeService =ServiceBeanFactory.get(InfoResume_age.class,null);

	/**
	 * 信息量
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-25
	 */
	@Path({"/trend"})
	@Check("dashboard/info/trend")
	@GET
	@POST
	public ActionResult dashboard() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			String maxDate = infoService.getMaxDate(String.format("business_name='%s'",cateEnum.getDBName()));
//			String maxDate = "2015-07-21";
			List<List<Object>> list = DatainfoBLL.getGeneral(maxDate, cateEnum);
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("tableData", list);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			context.put("cateTwoList",InfoConst.getCateListByBusiness(businessName));
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("info/trend");

	}
	
	/**
	 * 信息来源
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-25
	 */
	@Path({"/infoSource"})
	@Check("dashboard/info/infoSource")
	@GET
	@POST
	public ActionResult infoSource() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = infoService.getMaxDate(String.format("business_name='%s'",cateEnum.getDBName()));
//			String maxDate = "2015-07-21";
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("maxDate", maxDate);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
//			context.put("cateTwoList",InfoConst.getCateListByBusiness(businessName));
			context.put("platList",InfoSourceConst.platList);
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("info/infoSource");

	}
	
	/**
	 * 简历
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-7-25
	 */
	@Path({"/resume"})
	@Check("dashboard/info/resume")
	@GET
	@POST
	public ActionResult resume() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = infoResumeService.getMaxDate();
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("maxDate", maxDate);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("cateTwoList",InfoConst.resumeCateList);
			context.put("platList",InfoSourceConst.platList);
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("info/resumeBoard");
		
	}
	
	
	/**
	 * 发布成功率
	 * @author zhaoxiang 2015-7-28
	 */
	@Path({"/postRate"})
	@Check("dashboard/info/postRate")
	@GET
	@POST
	public ActionResult postRate() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			//检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			if(StringUtils.equals(businessName, "total")){
				businessName="zhaopin";
			}
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = infoService.getMaxDate(String.format("business_name='%s'",cateEnum.getDBName()));
//			String maxDate = "2015-07-21";
			
			Map<String, Object> context = beat.getModel().getModel();
			context.put("maxDate", maxDate);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("cateTwoList",InfoConst.getPostRateCateList(cateEnum.getDBName()));
		} catch (Exception e) {
			return ActionResult.view("null");
		}
		return ActionResult.view("info/postRate");
		
	}
	
}
